package com.ihrm.company.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/company/department")
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private CompanyService companyService;
	/**
	 * 添加部门
	 */
	@PostMapping
	public Result add(@RequestBody Department  department) {
		//设置保存的企业id:目前使用固定值1 companyId
/*		String companyId = "1";*/
		department.setCompanyId(companyId);
		departmentService.save(department);
		return Result.SUCCESS();
	}

	/**
	 * 根据id查询部门
	 */
	@GetMapping("/{departmentId}")
	public Result findById(@PathVariable(value = "departmentId") String id) {
		Department department = departmentService.findById(id);
		return new Result(ResultCode.SUCCESS,department);
	}

	/**
	 * 根据id更新部门
	 */
	@PutMapping("/{departmentId}")
	public Result update(@PathVariable(value = "departmentId") String id, @RequestBody Department department) {
		department.setId(id);
		departmentService.update(department);
		return Result.SUCCESS();
	}

	/**
	 * 查询所有部门列表
	 */
	@GetMapping
	public Result findAll() {
		//1.指定企业id
/*		String companyId = "1";*/
		List<Department> list = departmentService.findAll(companyId);
		//构造返回结果
		Company company = companyService.findById(companyId);
		DeptListResult deptListResult = new DeptListResult(company,list);
		return new Result(ResultCode.SUCCESS,deptListResult);
	}

	/**
	 * 根据id删除部门
	 */
	@DeleteMapping("/{departmentId}")
	public Result delete(@PathVariable(value = "departmentId") String id) {
		departmentService.deleteById(id);
		return Result.SUCCESS();
	}
}
